package api;
import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.internals.DefaultPartitioner;
import org.apache.kafka.common.serialization.StringSerializer;
public class KafkaProducerDemo {
    public static void main(String[] args) {
    	Properties props=new Properties();
    	props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.196.128:9092,192.168.196.128:9093,192.168.196.128:9094");
    	props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ObjectSerializer.class);
    	props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ObjectSerializer.class);
    	props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, CustomParititioner.class);
    	KafkaProducer<String, Order> kafkaProducer=new KafkaProducer<String, Order>(props);
    	/*ProducerRecord<String, Order> record=new ProducerRecord<String, Order>("topic02",
				"1005", new Order(1,"item01",100.0));*/
		/*ProducerRecord<String, Order> record=new ProducerRecord<String, Order>("topic02",
				1,"1005", new Order(1,"item01",100.0));*/
		for(int i=0;i<10;i++){
			ProducerRecord<String, Order> record=new ProducerRecord<String, Order>("topic01",
					null,new Order(i,"item0"+i,100.0));
			kafkaProducer.send(record);
			kafkaProducer.flush();
		}
    	kafkaProducer.close();
    	System.out.println("发送完....");
	}
}
